package Client;

import java.io.IOException;

import com.lloseng.ocsf.client.AbstractClient;
/**
 * connect to server
 * @author Termchai 5510546042, Eknarin 5510546239
 *
 */
public class Client extends AbstractClient {

	private Turn player;
	private Game game;
	private MesSelect mesSelect;
	private GUI gui;

	/**
	 * 
	 * @param host ip
	 * @param port number of port
	 */
	public Client(String host, int port) {
		super(host, port);
		game = new Game();
		mesSelect = new MesSelect(new int[] {0,0});
		// TODO Auto-generated constructor stub
	}

	
	@Override
	protected void handleMessageFromServer(Object msg) {
		System.out.println(msg);
		System.out.println(mesSelect.getClass()+"");
		System.out.println(msg.getClass()+"");

		if (msg.getClass() == "s".getClass()) {
			if (((String) msg).equals("P1")) {
				player = Turn.P1;
			}
			if (((String) msg).equals("P2")) {
				player = Turn.P2;
			}
			System.out.println("Set Player already");
		}

		if (msg.getClass() == game.getClass())
		{
			System.out.println("recive game");
			game=(Game)msg;
			GameConsole gameConsole = new GameConsole(game, "Player 1","Player 2");
			try {
				gui = new GUI (gameConsole, player,this);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if (msg.getClass() == mesSelect.getClass())
		{
			System.out.println("Recive MesSelect from server");
			MesSelect m = (MesSelect)msg;
			System.out.println(m.getLocation()[0]+"");
			System.out.println(m.getLocation()[1]+"");
			gui.getCC()[m.getLocation()[0]][m.getLocation()[1]].getClickAction().mouseClicked(null);
		}
		
		
		System.out.println("This is " + player);
	}

	/**
	 * test connection.
	 */
	public void talk() {
		try {
			sendToServer("TEST");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
